﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.IO;
using AlocaProfeV2.Modelos;

namespace AlocaProfeV2.Dao
{
    public class ProfessorDao
    {

        String xml_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xml/professor.xml");

        public void cadastraProfessor(Professor professor)
        {
            XDocument xmlprofessor = XDocument.Load(xml_path);

            //Descobrindo o ultimo id e definindo o id a ser criado
            Int32 ultimo_id = Convert.ToInt32(xmlprofessor.Descendants("Professores").Elements("Professor").Last().Attribute("id").Value);
            ultimo_id += 1;

            xmlprofessor.Element("Professores").Add(
                new XElement("Professor",
                    new XAttribute("id", ultimo_id), //TODO : Auto incremento
                    new XElement("Nome", professor.Nome),
                    new XElement("Email", professor.Email),
                    new XElement("Senha", professor.Senha),
                    new XElement("Prontuario", professor.Prontuario),
                    new XElement("RegimeTrabalho", professor.RegimeTrabalho),
                    new XElement("Contrato", professor.Contrato),
                    new XElement("Area"),
                    new XElement("Telefone", professor.Telefone),
                    new XElement("Celular", professor.Celular),
                    new XElement("Apelido", professor.Apelido),
                    new XElement("Titulo", professor.Titulo),
                    new XElement("Vinculo", professor.Vinculo),
                    new XElement("Semestre", professor.Semestre),
                    new XElement("Disciplina")
                    )
            );
            xmlprofessor.Save(xml_path);
        }

        public void alteraProfessor(Professor professor)
        {
            XElement xml = XElement.Load(xml_path, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);

            IEnumerable<XElement> elements = xml.Elements();

            foreach (var items in elements.Attributes("id").Where(e => e.Value == professor.Id.ToString()))
            {
                items.Parent.Element("Nome").Value = professor.Nome;
                items.Parent.Element("Email").Value = professor.Email;
                items.Parent.Element("Senha").Value = professor.Senha;
                items.Parent.Element("Prontuario").Value = professor.Prontuario;
                items.Parent.Element("RegimeTrabalho").Value = professor.RegimeTrabalho;
                items.Parent.Element("Contrato").Value = professor.Contrato;
                items.Parent.Element("Telefone").Value = professor.Telefone;
                items.Parent.Element("Celular").Value = professor.Celular;
                items.Parent.Element("Apelido").Value = professor.Apelido;
                items.Parent.Element("Titulo").Value = professor.Titulo;
                items.Parent.Element("Vinculo").Value = professor.Vinculo;
                items.Parent.Element("Semestre").Value = professor.Semestre.ToString();
            }

            //Salva Alterações
            xml.Save(xml_path);
        }

        public Modelos.Professor retornaProfessor(Int32 id)
        {
            Modelos.Professor professor = new Professor();
            XElement xml = XElement.Load(xml_path, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);

            IEnumerable<XElement> elements = xml.Elements();
             foreach (var items in elements.Attributes("id").Where(e => e.Value == id.ToString()))
            {
                           professor.Nome = items.Parent.Element("Nome").Value;
                           professor.Email = items.Parent.Element("Email").Value;
                           professor.Senha = items.Parent.Element("Senha").Value;
                           professor.Prontuario = items.Parent.Element("Prontuario").Value;
                           professor.RegimeTrabalho = items.Parent.Element("RegimeTrabalho").Value;
                           professor.Contrato = items.Parent.Element("Contrato").Value;
                           professor.Telefone = items.Parent.Element("Telefone").Value;
                           professor.Celular = items.Parent.Element("Celular").Value;
                           professor.Apelido = items.Parent.Element("Apelido").Value;
                           professor.Titulo = items.Parent.Element("Titulo").Value;
                           professor.Vinculo = items.Parent.Element("Vinculo").Value;
                           professor.Semestre = Convert.ToInt32(items.Parent.Element("Semestre").Value);
                       };
            return professor;
        }

        //Login
        public Modelos.Professor loginProfessor(String email, String senha)
        {
            XDocument xmlprofessor = XDocument.Load(xml_path);
            Modelos.Professor professor = new Professor();
            var data = from item in xmlprofessor.Descendants("Professor")
                       select new
                       {
                           id = item.Attribute("id").Value,
                           nome = item.Element("Nome").Value,
                           email = item.Element("Email").Value,
                           senha = item.Element("Senha").Value
                       };

            foreach (var prof in data)
            {
                if (email == prof.email && senha == prof.senha)
                {
                    professor.Id = Convert.ToInt32(prof.id);
                    professor.Nome = prof.nome;
                }
            }

            return professor;
        }

        public void atualizaMaterias(Modelos.Professor professor)
        {
            XElement xml = XElement.Load(xml_path, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
            XDocument xmlprofessor = XDocument.Load(xml_path);
            IEnumerable<XElement> elements = xml.Elements();

            foreach (var items in elements.Attributes("id").Where(e => e.Value == professor.Id.ToString()))
            {
                for (int cont = 0; cont < professor.Disciplinas.Count; cont++)
                {
                    items.Parent.Element("Disciplina").Add(new XElement("Id_Disciplina", new XAttribute("opcao", professor.Opcoes[cont]), professor.Disciplinas[cont]));
                }

            }

            String xml_path2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xml/disciplina.xml");
            XElement xml2 = XElement.Load(xml_path2, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
            IEnumerable<XElement> elements2 = xml2.Elements();

            for (int cont = 0; cont < professor.Disciplinas.Count; cont++)
            {
                foreach (var items2 in elements2.Attributes("id").Where(e => e.Value == professor.Disciplinas[cont].ToString()))
                {
                    if (professor.Opcoes[cont] == "Gostaria de lecionar")
                    {
                        items2.Parent.Element("Alocada").Value = "True";
                    }
                }
            }
            //Salva Alterações
            xml.Save(xml_path);
            xml2.Save(xml_path2);
        }
    }
}